/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Main;

/**
 *
 * @author Usuario
 */

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class AdministrarUsuarios extends JFrame implements ActionListener{

    private JLabel lbLogin, lbUserName, lbPass, lbImage, lbRol, suImage;
    private JTextField tfUserName;
    private JComboBox cbLogin, cbRol, cbImages;
    private JPasswordField pfcontra;
    private JButton btnGuardar, btnEliminar, btnRegistrar;
    private JSeparator separador;
    private Font fuenteLetra;
    private Color colorLetra;
    private String[] stringImagenes, stringLogins, stringRol;
    private JPanelConFondo panelFondo ;
    private AccesoDatosUsuario accesoDatos;
   
    public AdministrarUsuarios(){

        super("Administrar Jugadores");

        colorLetra = Color.LIGHT_GRAY;
        fuenteLetra = new Font("Arial", Font.BOLD, 12);
        accesoDatos = new AccesoDatosUsuario();

       stringLogins =  accesoDatos.ConsultarLogins();
       cbLogin = new JComboBox(stringLogins);
       cbLogin.addActionListener(this);

        stringImagenes = new String[]{"Perfil_1", "Perfil_2", "Perfil_3", "Perfil_4", "Perfil_5", "Perfil_6",
        "Perfil_7", "Perfil_8", "Perfil_9", "Perfil_10", "Perfil_11", "Perfil_12"};
        cbImages = new JComboBox(stringImagenes);
        cbImages.addActionListener(this);

        stringRol = new String[]{"Jugador","Administrador"};
        cbRol = new JComboBox(stringRol);
//        cbLogin.setSelectedIndex(-1);
//        cbRol.setSelectedIndex(-1);
//        cbImages.setSelectedIndex(-1);

    
        lbLogin = new JLabel("Login");
        lbUserName = new JLabel("Nombre Jugador");
        lbPass = new JLabel("Contraseña");
        lbRol = new JLabel("Rol");
        suImage = new JLabel("Su Imagen");

        lbImage = new JLabel();
        lbImage.setHorizontalAlignment(JLabel.CENTER);
        escogerImagen(stringImagenes[cbImages.getSelectedIndex()]);
        lbImage.setOpaque(false);

        tfUserName = new JTextField();
        pfcontra = new JPasswordField();

        btnGuardar = new JButton("Guardar Cambios");
        btnEliminar = new JButton("Eliminar");
        btnRegistrar = new JButton("Registrar");
        separador = new JSeparator();
     
        lbLogin.setForeground(colorLetra);
        lbLogin.setFont(fuenteLetra);
        lbUserName.setForeground(colorLetra);
        lbUserName.setFont(fuenteLetra);
        lbPass.setForeground(colorLetra);
        lbPass.setFont(fuenteLetra);
        lbImage.setForeground(colorLetra);
        lbImage.setFont(fuenteLetra);
        lbRol.setForeground(colorLetra);
        lbRol.setFont(fuenteLetra);

        
        Box vertical2 = Box.createVerticalBox();
        Box horizontal = Box.createHorizontalBox();

        vertical2.add(Box.createVerticalStrut(5));
        vertical2.add(Box.createHorizontalStrut(5));
        vertical2.add(lbLogin);
        vertical2.add(cbLogin);
        vertical2.add(btnRegistrar);
        vertical2.add(Box.createVerticalStrut(10));
        vertical2.add(separador);

        JPanel panel = new JPanel(new GridLayout(3,2));
        panel.setOpaque(false);
        panel.add(lbUserName);
        panel.add(tfUserName);
        panel.add(lbPass);
        panel.add(pfcontra);
        panel.add(lbRol);
        panel.add(cbRol);

        vertical2.add(Box.createHorizontalStrut(5));
        vertical2.add(Box.createVerticalStrut(20));
        vertical2.add(panel);
     
        JPanel panelBoton = new JPanel(new GridLayout(1,2));
        panelBoton.add(btnGuardar);
        panelBoton.add(btnEliminar);
        vertical2.add(Box.createVerticalStrut(60));
        vertical2.add(Box.createHorizontalStrut(10));
        vertical2.add(panelBoton);
        Box vertical3 = Box.createVerticalBox();
        vertical3.add(Box.createHorizontalStrut(40));
        vertical3.add(Box.createVerticalStrut(80));
        vertical3.add(cbImages);
        vertical3.add(Box.createHorizontalStrut(20));
        vertical3.add(lbImage);
              
        Image imagen=  new ImageIcon("Imagenes/admin_User.jpg").getImage();
        panelFondo = new JPanelConFondo(imagen);
        panelFondo.add(vertical2);
        panelFondo.add(vertical3);
        panelFondo.add(horizontal);
        
        Container c = getContentPane();
        c.add(panelFondo);

        this.setUndecorated(true);
        this.getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
        this.setBounds(260, 160, 550, 350);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

       private void escogerImagen(String name){

        ImageIcon icono = new ImageIcon ("Imagenes/Perfiles/"+name+".jpg");
        lbImage.setIcon(new ImageIcon(icono.getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)));
        lbImage.setOpaque(false);

    }

    public void actionPerformed(ActionEvent e) {

          if (e.getSource() == cbLogin) {
            if (cbLogin.getSelectedItem() != null) {
                int perfil = 0;
                AccesoDatosUsuario acceso = new AccesoDatosUsuario();
                Usuario user = acceso.ConsultarUsuario((String)cbLogin.getSelectedItem());
                tfUserName.setText(user.getNombre());
                pfcontra.setText(user.getPass());
                cbImages.setSelectedIndex(user.getImagen());
                escogerImagen(stringImagenes[cbImages.getSelectedIndex()]);

                if (user.getRol() == 1) {
                    perfil = 1;
                }
                cbRol.setSelectedIndex(perfil);
            } else {
//                tfUserName.setText("");
//                pfcontra.setText("");
//                cbRol.setSelectedIndex(-1);
            }
        }
    }

    public static void main(String args[]){
        AdministrarUsuarios au = new AdministrarUsuarios();
    }



}
